﻿/*
=======================================================
    Warehouse【仓库】
=======================================================
*/

using Entities;

namespace Services
{
    public interface IWarehouseRepository : IRepository<Warehouse>
    {
        int UpdateIsDelete(bool flag, int[] ids);
    }


    public class WarehouseRepository : BaseRepository<Warehouse>, IWarehouseRepository
    {
        private readonly ICreateCode _ICreateCode;

        public WarehouseRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<Warehouse> query, ICreateCode iCreateCode)
            : base(dB, currentUser, query)
        {
            _ICreateCode = iCreateCode;
        }

        #region 启用/禁用  UpdateIsDelete
        public int UpdateIsDelete(bool flag, int[] ids)
        {
            return _DB.Updateable<Warehouse>()
                      .SetColumns(x => new Warehouse { IsDelete = flag })
                      .Where(x => ids.Contains(x.WarehouseID))
                      .ExecuteCommand();
        }
        #endregion

        #region Insert
        public override int Insert(Warehouse entity)
        {
            int id = base.Insert(entity);

            //添加系统库位
            _DB.Insertable(new WarehousePosition
            {
                GroupInfoID = entity.GroupInfoID,
                WarehouseID = id,
                Code = _ICreateCode.GetWarehousePositionCode(),
                Name = "系统库位",
                IsSystem = true,
                IsBlank = true,
                IsDelete = false,
                CreateUserID = entity.CreateUserID,
                CreateUserName = entity.CreateUserName,
                CreateDate = entity.CreateDate,
                UpdateUserID = entity.UpdateUserID,
                UpdateUserName = entity.UpdateUserName,
                UpdateDate = entity.UpdateDate,
            }).ExecuteCommand();

            if (entity.UserIDs.IsNotEmpty() && entity.UserIDs.Count > 0)
            {
                var users = entity.UserIDs.Select(x => new WarehouseUser
                {
                    GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID,
                    WarehouseID = id,
                    UserID = x
                }).ToList();

                _DB.Insertable(users).ExecuteCommand();
            }

            return id;
        }
        #endregion

        #region Update
        public override int Update(Warehouse entity)
        {
            int n = base.Update(entity);

            _DB.Deleteable<WarehouseUser>().Where(x => x.WarehouseID == entity.WarehouseID).ExecuteCommand();
            if (entity.UserIDs.IsNotEmpty() && entity.UserIDs.Count > 0)
            {
                var users = entity.UserIDs.Select(x => new WarehouseUser
                {
                    GroupInfoID = _currentUser.CurrentUserInfo.LoginTypeID,
                    WarehouseID = entity.WarehouseID,
                    UserID = x
                }).ToList();

                _DB.Insertable(users).ExecuteCommand();
            }

            return n;
        }
        #endregion

        #region GetByID
        public override Warehouse GetByID(int id)
        {
            var entity = base.GetByID(id);
            entity.UserIDs = _DB.Queryable<WarehouseUser>().Where(x => x.WarehouseID == id).Select(x => x.UserID).ToList();
            return entity;
        }
        #endregion
    }
}
